﻿layui.define(['jquery', 'form','common'], function (exports) {
    var $ = layui.jquery;
    var form = layui.form;
    var com = layui.common;
    var skill = {
        getNames: function (kind, userId) {
            getPost('/Skill/SkillNames', {
                kind: kind
            }, function (sel) {
                getPost('/Skill/SkillEntity', {
                    kind: kind,
                    userId: userId
                }, function (arr) {
                    var res = arr.msg;
                    var html = '<div class="layui-form-item">';
                    var tab1 = '';
                    var tab2 = '';
                    for (var i = 0; i < res.length - 1; i++) {
                        if ((kind != "JapLang"  || i < 4) && (kind != "Experience") ) {
                            html += '<div class="layui-inline">'
                            html += '<label class="layui-form-label" style="width:175px">' + res[i].des + '</label>';
                            html += '<div class="layui-input-block" style="width:150px;margin-left:175px">'
                            html += '<select id="' + res[i].txt + '" name="' + res[i].txt + '" ></select>'
                            html += '</div>';
                            html += '</div>';
                        } else {
                            html += '<label class="layui-form-label" style="width:175px">' + res[i].des + '</label>';
                            html += '<div class="layui-input-inline" style="width:150px;">';
                            html += '<input id="' + res[i].txt + '" name="' + res[i].txt + '" class="layui-input" required lay-verify="number" />';
                            html += '</div>';
                        }
                        if (i > 0 && (i % 2 == 1 || i == res.length - 1)) {
                            html += '</div>';
                            html += '<div class="layui-form-item">';
                        }
                        if (kind != "JapLang" && kind != "Experience" && res[i].val == '2') {
                            tab1 += '<tr><td style="background-color:#FBFBFB;width:175px">' + res[i].des + '</td><td class="layui-btn-warm" style="width:45px">熟练' + '</td>';
                        } else if (kind != "JapLang" && res[i].val == '3') {
                            tab2 += '<tr><td style="background-color:#FBFBFB;width:175px">' + res[i].des + '</td><td class="layui-btn-danger" style="width:45px">精通' + '</td>';
                        }
                    }
                    $("#Skilled").html(tab1);
                    $("#Adept").html(tab2);
                    form.render();

                    html += '</div>';
                    $("#skillKind").text(skill.setLegend(kind));
                    $(".layui-field-box").html(html);
                    $("select").each(function () {
                        if ($(this).attr("id") != "Level") {
                            var html1 = '';
                            var html2 = '';
                            if (sel.status == 0) {
                                var names = sel.msg;
                                for (var i = 0; i < names.length; i++) {
                                    if ($(this).attr("id") == names[i].val) {
                                        html1 = '<dd lay-value="0" >' + "不具备" + '</dd>';
                                        html2 = '<option value="0" >' + "不具备" + '</option>';
                                        html1 += '<dd lay-value="1" >' + "了解" + '</dd>';
                                        html2 += '<option value="1" >' + "了解" + '</option>';
                                        html1 += '<dd lay-value="2" >' + "熟练" + '</dd>';
                                        html2 += '<option value="2" >' + "熟练" + '</option>';
                                        html1 += '<dd lay-value="3" >' + "精通" + '</dd>';
                                        html2 += '<option value="3" >' + "精通" + '</option>';
                                    }
                                }
                            } else {
                                html1 = '<dd lay-value="" >' + "请选择" + '</dd>';
                                html2 = '<option value="" >' + "请选择" + '</option>';
                            }
                        } else {
                            html1 = '<dd lay-value="N1" >' + "N1" + '</dd>';
                            html2 = '<option value="N1" >' + "N1" + '</option>';
                            html1 += '<dd lay-value="N2" >' + "N2" + '</dd>';
                            html2 += '<option value="N2" >' + "N2" + '</option>';
                            html1 += '<dd lay-value="N3" >' + "N3" + '</dd>';
                            html2 += '<option value="N3" >' + "N3" + '</option>';
                            html1 += '<dd lay-value="N4" >' + "N4" + '</dd>';
                            html2 += '<option value="N4" >' + "N4" + '</option>';
                            html1 += '<dd lay-value="N5" >' + "N5" + '</dd>';
                            html2 += '<option value="N5" >' + "N5" + '</option>';
                            html1 += '<dd lay-value="社内L1" >' + "社内L1" + '</dd>';
                            html2 += '<option value="社内L1" >' + "社内L1" + '</option>';
                            html1 += '<dd lay-value="社内L2" >' + "社内L2" + '</dd>';
                            html2 += '<option value="社内L2" >' + "社内L2" + '</option>';
                            html1 += '<dd lay-value="社内L3" >' + "社内L3" + '</dd>';
                            html2 += '<option value="社内L3" >' + "社内L3" + '</option>';
                            html1 += '<dd lay-value="社内L4" >' + "社内L4" + '</dd>';
                            html2 += '<option value="社内L4" >' + "社内L4" + '</option>';
                        }
                        $(this).next().children().eq(1).html(html1);
                        $(this).html(html2);
                    });

                    for (var i = 0; i < res.length; i++) {
                        $("#" + res[i].txt).val(res[i].val);
                    }
                    if (userId != layui.data("user").user.uid) {
                        $("select").attr("disabled", "disabled");
                        $(".layui-input").attr("disabled", "disabled");
                        $("#btnSave").css("display", "none");
                    }
                    if (kind === "JapLang") {
                        $(".user-looklog").css("display", "none");
                    } else {
                        $(".user-looklog").css("display", "block");
                    }
                    $("form").attr("action", "/skill/" + kind);
                    form.render();
                });
            });
        }        
        , setLegend: function (kind) {
            switch (kind) {
                case "OSSkill":
                    return "操作系统";
                case "ServerSkill":
                    return "服务器";
                case "DBOSkill":
                    return "Open系数据库";
                case "DBHSkill":
                    return "Host系数据库";
                case "PhoneSkill":
                    return "移动端";
                case "HostLang":
                    return "Host系开发语言";
                case "OpenLang":
                    return "Open系开发语言";
                case "PageSkill":
                    return "前端开发语言";
                case "ScriptLang":
                    return "脚本语言";
                case "ReportSkill":
                    return "报表(帐票)";
                case "DBLink":
                    return "数据库链接";
                case "OtherLang":
                    return "其他开发语言";
                default:
                    return "日语能力";
            }
        }       
    };
    
    form.on('select(SkillKind)', function (data) {
        if (data.value != "") {
            com.getOption('/Skill/SkillNames', data.value, "SkillName");
        } else {
            var html1 = '<dd lay-value="" >' + "请选择" + '</dd>';
            var html2 = '<option value="" >' + "请选择" + '</option>';
            $("#SkillName").next().children().eq(1).html(html1);
            $("#SkillName").html(html2);
        }
    });

    exports('skill', skill);
});